home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c,comp.std.c
- Path: uu4news.netcom.com!friend!news
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Exercise: Help me write a portable sleep() function
- Message-ID: <1996Mar4.164124.22845@friend.kastle.com>
- Sender: news@friend.kastle.com (News)
- Reply-To: rich@kastle.com
- Organization: Kastle Development Associates
- X-Newsreader: Forte Free Agent 1.0.82
- Date: Mon, 4 Mar 1996 16:42:33 GMT
-
- As an exercise, I'm trying to write a fully Standard-conforming
- portable sleep() function. I want to make sure it has no undefined
- behavior, and it should detect and deal with implementation-specific
- behavior.
-
- It need not work, only "most probably" work. :-)
-
- Please post corrections and comments, do not just email me. I'd like
- this exercise to be open to the whole public.
-
- Here is that I have so far. I tried this under Windows NT, compiled
- with MSVC 4.0, and I got the behavior I expected.
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <limits.h>
-
- /*
- YIELD macro
-
- Perhaps your execution environment may have some means to show
- intent to offer computing time to other processes. If so, and if
- your compiler allows you to create preprocessor definitions without
- embedding them in this source (rumors are that these are called
- "command line definitions", and I suppose the Standard does not
- forbid this) then a YIELD definition might somehow be provided that
- invokes this procedure.
-
- */
-
- #ifndef YIELD
- #define YIELD /* Of course, fully Standard code has
- no such function. */
- #endif
-
- /*
- Following is mysleep_time. This function uses the time() function
- call to delay for the given amount of time. If the time() services
- are not provided by this environment, it returns 0; otherwise, it
- returns 1 after at least the given amount of time has elapsed.
-
- It is unfortunate that no means is available to determine if the
- desired amount of delay time cannot be represented by the provided
- timing services.
-
- */
-
- int mysleep_time(int duration)
- {
- time_t start; /* The time at which timing began */
-
- if((start = time(NULL)) == -1)
- return 0; /* Sorry, the environment does not
- provide this timing-related
- service. */
-
- while(difftime(time(NULL), start) <= (double)duration)
- YIELD;
-
- return 1; /* Time delay has elapsed. */
- }
-
- /*
- The following is mysleep_clock. This function makes use of the
- clock() service, perhaps to provide a more accurate timing interval,
- and perhaps to overcome an environment shortcoming that time() is
- not provided.
- */
-
- int mysleep_clock(int duration)
- {
- clock_t start;
-
- /* I cannot make any assumption regarding the value of
- CLOCKS_PER_SEC if the target environment cannot support
- clock(). Perhaps it may be zero or negative. I must code to
- prevent the compiler from executing a constant division by
- CLOCKS_PER_SEC to avoid the possibility that my program fail to
- compile in this fully-Standard environment. */
-
- if(CLOCKS_PER_SEC <= 0)
- return 0; /* This environment obviously cannot
- support clock(). */
-
- {
- int duration_max = INT_MAX;
- duration_max /= CLOCKS_PER_SEC;
- if(duration >= duration_max)
- return 0;
- }
-
- if((start = clock()) == -1)
- return 0; /* The environment does not provide
- such timing services */
-
- duration *= CLOCKS_PER_SEC; /* Scale duration from seconds to
- become clock ticks */
-
- while(clock() - start <= duration)
- YIELD;
-
- return 1;
- }
-
- /*
- Here is the caller's entry point. The argument is the number of
- seconds of sleep desired. The return values are 1 if the delay has
- occurred, and 0 if no timing services could be found. The caller
- should check for this possible failure.
-
- This function will delay for at least the amount of time requested.
- It may delay for an unspecified extra amount of time, depending on
- the resolution of system timing services and the possible scheduling
- of other programs by the execution environment.
- */
-
- int mysleep(int duration)
- {
- /* Try the more accurate version first */
-
- if(mysleep_clock(duration))
- return 1; /* Timing success. */
-
- return mysleep_time(duration); /* Resort to the time()-based
- version */
- }
-
- --
- Richard Krehbiel, Kastle Systems, Arlington VA USA
- rich@kastle.com (work) or richk@mnsinc.com (personal)
-
-